package com.yoomiito.app.adapter.home.reommend;

import android.content.Context;
import androidx.annotation.NonNull;

import androidx.recyclerview.widget.RecyclerView;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.yoomiito.app.R;
import com.yoomiito.app.model.bean.BannerAndIconInfo;
import com.yoomiito.app.utils.ColorUtils;
import com.yoomiito.app.utils.DensityUtil;
import com.yoomiito.app.utils.ImageLoader;

import java.util.List;

/**
 * 首页---活动
 */
public class HomeActAdapter extends DelegateAdapter.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private LayoutHelper mLayoutHelper;
    private List<BannerAndIconInfo> datas;
    private OnItemClickLisenter mOnItemClickLisenter;

    public HomeActAdapter(Context context, LayoutHelper layoutHelper, List<BannerAndIconInfo> datas) {
        mContext = context;
        mLayoutHelper = layoutHelper;
        this.datas = datas;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.home_item_activity,parent,false);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
        if (position%2==0){
            if (position==0){
                itemViewHolder.bg.setPadding(DensityUtil.dp2px(12f),DensityUtil.dp2px(12f),DensityUtil.dp2px(5.5f),DensityUtil.dp2px(5f));
            }else if(position==datas.size()-2){
                itemViewHolder.bg.setPadding(DensityUtil.dp2px(12f),DensityUtil.dp2px(5f),DensityUtil.dp2px(5.5f),DensityUtil.dp2px(12f));
            }else {
                itemViewHolder.bg.setPadding(DensityUtil.dp2px(12f),DensityUtil.dp2px(5f),DensityUtil.dp2px(5.5f),DensityUtil.dp2px(5f));
            }
        }else{
            if (position==1){
                itemViewHolder.bg.setPadding(DensityUtil.dp2px(5.5f),DensityUtil.dp2px(12f),DensityUtil.dp2px(12f),DensityUtil.dp2px(5f));
            }else if(position==datas.size()-1){
                itemViewHolder.bg.setPadding(DensityUtil.dp2px(5.5f),DensityUtil.dp2px(5f),DensityUtil.dp2px(12f),DensityUtil.dp2px(12f));
            }else {
                itemViewHolder.bg.setPadding(DensityUtil.dp2px(5.5f),DensityUtil.dp2px(5f),DensityUtil.dp2px(12f),DensityUtil.dp2px(5f));
            }
        }
        BannerAndIconInfo info = datas.get(position);
        itemViewHolder.bg.setBackgroundColor(Color.parseColor(info.getBgcolor()));
        ImageLoader.getInstance().into(mContext,info.getImageUrl(),itemViewHolder.tabIv,R.drawable.default_banner_1);
        itemViewHolder.tabIv.setOnClickListener(v -> {
            if (mOnItemClickLisenter!=null){
                mOnItemClickLisenter.onClick(info);
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas==null?0:datas.size();
    }

    public void newDatas(List<BannerAndIconInfo> datas){
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return 14;
    }

    private class ItemViewHolder extends RecyclerView.ViewHolder {

        public ImageView tabIv;
        private LinearLayout bg;
        public ItemViewHolder(View itemView) {
            super(itemView);
            tabIv = itemView.findViewById(R.id.iv_activity_one);
            bg = itemView.findViewById(R.id.bg);
        }
    }

    public void setOnItemClickLisenter(OnItemClickLisenter onItemClickLisenter) {
        mOnItemClickLisenter = onItemClickLisenter;
    }

    public interface OnItemClickLisenter {
        void onClick(BannerAndIconInfo info);
    }
}
